package com.example.databasepj.service;

import com.example.databasepj.dto.NotExistResponse;
import com.example.databasepj.dto.UserData;
import com.example.databasepj.dtoAdvanced.UserStarAnalysisRequest;
import com.example.databasepj.dtoAdvanced.UserStarAnalysisResponse;
import com.example.databasepj.entity.UserEntity;
import com.example.databasepj.entity.UserStarDishEntity;
import com.example.databasepj.entity.UserStarMerchantEntity;
import com.example.databasepj.exception.BusinessException;
import com.example.databasepj.exception.ErrorCode;
import com.example.databasepj.mapper.UserMapper;
import com.example.databasepj.mapper.UserStarDishMapper;
import com.example.databasepj.mapper.UserStarMerchantMapper;
import org.apache.ibatis.exceptions.PersistenceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserStarMerchantMapper userStarMerchantMapper;
    @Autowired
    private UserStarDishMapper userStarDishMapper;
    public List<UserEntity> getAll(){
        return userMapper.getAll();
    }

    //收藏商户
    public NotExistResponse userStarMerchant(int user_ID, int merchant_ID){
        //先看是否已收藏
        UserStarMerchantEntity userStarMerchantEntity = userStarMerchantMapper.selectByUserAndMerchant(user_ID,merchant_ID);
        //如果已收藏
        if(userStarMerchantEntity!=null){
            return new NotExistResponse(true);
        }
        //如果未收藏
        try {
            userStarMerchantMapper.insertStarMerchant(new UserStarMerchantEntity(user_ID,merchant_ID));
            return new NotExistResponse(false);
            // 插入成功后的逻辑
        } catch (PersistenceException e) {
            // 记录异常信息
//            ex.printStackTrace();
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);

        }

    }

    //取消收藏商户
    public NotExistResponse userCancelStarMerchant(int user_ID, int merchant_ID){
        //先看是否已收藏
        UserStarMerchantEntity userStarMerchantEntity = userStarMerchantMapper.selectByUserAndMerchant(user_ID,merchant_ID);
        //如果未收藏
        if(userStarMerchantEntity == null){
            return new NotExistResponse(false);
        }
        //如果已收藏，则取消收藏
        try {
            userStarMerchantMapper.deleteUserStarMerchant(new UserStarMerchantEntity(user_ID,merchant_ID));
            return new NotExistResponse(true);
            // 插入成功后的逻辑
        } catch (PersistenceException e) {
            // 记录异常信息
//            ex.printStackTrace();
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);

        }

    }

    //收藏菜品
    public NotExistResponse userStarDish(int user_ID, int dish_ID){
        //先看是否已收藏
        UserStarDishEntity userStarDishEntity=userStarDishMapper.selectByUserAndDish(user_ID,dish_ID);
        //如果已收藏
        if(userStarDishEntity != null){
            return new NotExistResponse(true);
        }
        //如果未收藏
        try {
            userStarDishMapper.insertStarDish(new UserStarDishEntity(user_ID,dish_ID));
            return new NotExistResponse(false);
            // 插入成功后的逻辑
        } catch (PersistenceException e) {
            // 记录异常信息
//            ex.printStackTrace();
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);

        }
    }

    //取消收藏菜品
    public NotExistResponse userCancelStarDish(int user_ID,  int dish_ID){
        //先看是否已收藏
        UserStarDishEntity userStarDishEntity=userStarDishMapper.selectByUserAndDish(user_ID,dish_ID);
        //如果未收藏
        if(userStarDishEntity == null){
            return new NotExistResponse(false);
        }
        //如果已收藏，则取消收藏
        try {
            userStarDishMapper.deleteUserStarDish(new UserStarDishEntity(user_ID,dish_ID));
            return new NotExistResponse(true);
            // 插入成功后的逻辑
        } catch (PersistenceException e) {
            // 记录异常信息
//            ex.printStackTrace();
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);

        }

    }
    //获取user详细信息
    public UserData getUserInfo(int userId){
        UserEntity userEntity = userMapper.selectUserById(userId);
        UserData userData=new UserData(userEntity.getUser_id(), userEntity.getUser_name(), userEntity.getGender(),userEntity.getStudentOrWorkNumber(),userEntity.getAge(),userEntity.getRole(),userEntity.getPassword());
        return userData;

    }
    //更新user信息
    public void updateUserInfo(UserData userData){
        UserEntity userEntity=new UserEntity(userData.getUser_ID(), userData.getUser_name(), userData.getGender(), userData.getStudentOrWorkNumber(),userData.getAge(), userData.getRole(),userData.getPassword());
        userMapper.updateUserData(userEntity);

    }

    public List<UserStarAnalysisResponse> userStarAnalysis(UserStarAnalysisRequest request){
        List<UserStarAnalysisResponse> results = new ArrayList<>();
        //如果对点餐方式无筛选
        if(request.getOrderForm()==2){
            if(request.getTime().equals("week")){
                results=userStarDishMapper.selectDishByWeek(request.getUser_ID());
            }else if(request.getTime().equals("month")){
                results=userStarDishMapper.selectDishByMonth(request.getUser_ID());

            }//year
            else {
                results=userStarDishMapper.selectDishByYear(request.getUser_ID());
            }

        }else {//如果对点餐方式有筛选
            if(request.getTime().equals("week")){
                results=userStarDishMapper.selectDishByWeekAndForm(request.getUser_ID(), request.getOrderForm());
            }else if(request.getTime().equals("month")){
                results=userStarDishMapper.selectDishByMonthAndForm(request.getUser_ID(),request.getOrderForm());

            }//year
            else {
                results=userStarDishMapper.selectDishByYearAndForm(request.getUser_ID(),request.getOrderForm());
            }

        }
        return results;
    }





}
